package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.entity.TProduct;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.TCustomer;
import com.enbatis.mybatisplugs.service.TCustomerService;


import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/t_customer")
public class TCustomerController extends BaseController {

@Autowired
private TCustomerService tCustomerService;


    @PostMapping("/add")
    public ResultReturn<TCustomer> add(@RequestBody TCustomer tCustomer){

        tCustomer.setCompanyId(getAccount().getCompanyId());
        tCustomer.setCtime(new Date());
        tCustomer.setUid(getAccount().getId());
        return  success(tCustomerService.insert(tCustomer));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(tCustomerService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody TCustomer tCustomer){
        tCustomer.setId(id);
        return success(tCustomerService.updateById(tCustomer));
    }


    @GetMapping("/{id}")
    public ResultReturn<TCustomer> get(@PathVariable("id") Long id) {
        return success(tCustomerService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<TCustomer>> getList(TCustomer tCustomer){
        return success(tCustomerService.list(new Wrapper<>(tCustomer)));
    }


    @GetMapping("/list_customer")
    public ResultReturn<List<TCustomer>> getListCustomer(TCustomer tCustomer){
        return success(tCustomerService.list(new Wrapper<>(tCustomer).setSqlSelect("id","name")));
    }


    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page")
    public ResultReturn<Pages<TCustomer>> getPage(@RequestBody TCustomer customer){
        Page<TCustomer> page= getPage();

       Wrapper wrapper= new Wrapper<>();

       if (StringUtils.isNotBlank(customer.getName())){
           wrapper.like("name",customer.getName());
           customer.setName(null);
       }
       if (StringUtils.isNotBlank(customer.getCode())){
           wrapper.like("code",customer.getCode());
           customer.setCode(null);
       }
        if (StringUtils.isNotBlank(customer.getContact())){
            wrapper.like("contact",customer.getContact());
            customer.setContact(null);
        }
        wrapper.setObj(customer);
        return success(tCustomerService.page(page,wrapper));
    }


}
